package dao.wechatlab.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.stereotype.Repository;

import dao.BaseDao;
import dao.wechatlab.DataSetDao;
import pojo.weichatlab.WechatLabel;

@Repository
@SuppressWarnings("unchecked")
public class DataSetDaoImpl extends BaseDao implements DataSetDao {

	@Override
	public boolean createTampTable() {
		String sqlalter = "truncate temp_wcn";
		String sqlcreate = "insert into TempLabel(wechatid,classid,num) SELECT wechatid,classid,sum(times) as num FROM WechatClassMap group by wechatid,classid";
		Session session = null;
		try {
			session = currentSession();
			Transaction tx = session.beginTransaction();

			this.getHibernateTemplate().getSessionFactory().getCurrentSession()
					.createSQLQuery(sqlalter).executeUpdate();
			Query query = session.createQuery(sqlcreate);
			query.executeUpdate();
			tx.commit();
			return true;
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			session.getTransaction().rollback();
			return false;
		}
	}

	@Override
	public boolean addWechatLabel(int topn) {
		/*
		 * this.jdbcTemplate.query("", new RowMapper(){
		 * 
		 * @Override public Object mapRow(ResultSet rs, int rowNum) throws
		 * SQLException { Object o = new Object(); rs.get // TODO Auto-generated
		 * method stub return null; }
		 * 
		 * });
		 */
		String sql1 = "truncate wechatfull";
		String sql3 = "insert into temp_wcn(wechatid,classid) select a.wechatid,a.classid from temp_wcn a where a.num = (select max(b.num) from temp_wcn b where a.wechatid=b.wechatid)";
		String sql4 = "insert into WechatLabel(wechatid, labelid, url, title,name,account,brief,content,date) select a.id,b.classid, a.url, a.title, a.name, a.account, a.brief , a.content, a.date from Wechat a, TempLabel b where a.id = b.wechatid and b.num = null";
		Session session = null;
		try {
			session = currentSession();
			Transaction tx = session.beginTransaction();

			this.getHibernateTemplate().getSessionFactory().getCurrentSession()
					.createSQLQuery(sql1).executeUpdate();
			this.getHibernateTemplate().getSessionFactory().getCurrentSession()
					.createSQLQuery(sql3).executeUpdate();
			Query query = session.createQuery(sql4);
			query.executeUpdate();
			tx.commit();
			return true;
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			session.getTransaction().rollback();
			return false;
		}
	}

	@Override
	public List<WechatLabel> selectWechatLabel() {

		List<WechatLabel> list = new ArrayList<WechatLabel>();
		try {
		//	String sql = "new WechatLabel(wechatid,classes,url,title,name,account,brief,content,date) select wechatid,group_concat(labelid),url,title,name,account,brief,content,date from WechatLabel group by wechatid";
			String sql = "select wechatid,group_concat(labelid) as classes,url,title,name,account,brief,content,date from wechatfull group by wechatid";
            List<Map<String,Object>> lists = this.jdbcTemplate.queryForList(sql);
	        for (Map map : lists) {  
	     //   	WechatLabel wl =  new WechatLabel();  
	         	WechatLabel wl = WechatLabel.toObject(map);
	            if (wl != null) {  
	            	list.add(wl);  
	            }  
	        }  
	        return list;
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw e;
		}

	}

}
